import {
	Entity,
	Column,
	PrimaryGeneratedColumn,
	CreateDateColumn,
	UpdateDateColumn,
	ManyToOne,
	JoinColumn,
} from "typeorm";
import { User } from "../../user/entities/user.entity";
import { DateTransformer } from "../../../common/transformers/date.transformer";

@Entity("addresses")
export class Address {
	@PrimaryGeneratedColumn()
	id: number;

	@Column()
	userId: number;

	@Column({ length: 50 })
	name: string;

	@Column({ length: 20 })
	phone: string;

	@Column({ length: 50 })
	province: string;

	@Column({ length: 50 })
	city: string;

	@Column({ length: 50 })
	district: string;

	@Column({ type: "text" })
	detail: string;

	@Column({ default: false })
	isDefault: boolean;

	@CreateDateColumn({
		type: "datetime",
		name: "created_at",
		transformer: new DateTransformer(),
		nullable: false,
	})
	createdAt: Date;

	@UpdateDateColumn({
		type: "datetime",
		name: "updated_at",
		transformer: new DateTransformer(),
		nullable: false,
	})
	updatedAt: Date;

	// 与用户的关系
	@ManyToOne(() => User, (user) => user.addresses)
	@JoinColumn({ name: "userId" })
	user: User;
}
